package com.lionxing.DNTG;

import java.util.HashMap;

import org.cocos2d.events.CCTouchDispatcher;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;


public class MainLayer extends CCLayer{
	
	int _goleve;
	
	public static CCScene scene() {
		CCScene scene = CCScene.node();
		MainLayer layer = new MainLayer();

		scene.addChild(layer);
		
		return scene;
	}

	
	protected MainLayer()
	{
		
		_goleve = getGoleve();
		
		CGSize size = CCDirector.sharedDirector().winSize();
		
		CCSprite bgSp = CCSprite.sprite("cover.png");
		
		GreateMenu(bgSp);
		
		
		bgSp.setScaleX((size.width/GameDefine.KIos_W));
		bgSp.setScaleY((size.height/GameDefine.KIos_h));
		bgSp.setPosition(CGPoint.make(size.width/2, size.height/2));
		//Log.
		addChild(bgSp, -1);
		
	}
	
	
	private void GreateMenu(CCSprite sp)
	{
		
		
		
		CCMenuItemImage playItemmenu = CCMenuItemImage.item("coverbu1a.png", "coverbu1.png", this,"gotoPlay");
		CCMenu menuplay = CCMenu.menu(playItemmenu);
		menuplay.setPosition(CGPoint.make(145, 180));
		sp.addChild(menuplay);
		
		
		CCMenuItemImage goOnplayItemmenu = CCMenuItemImage.item("coverbu2a.png", "coverbu2.png", "coverbu2b.png", this, "goONpaly");
		CCMenu menuGoOn = CCMenu.menu(goOnplayItemmenu);	
		menuGoOn.setPosition(CGPoint.make(55, 120));
		sp.addChild(menuGoOn);
		
		if(_goleve < 2)
		{
			goOnplayItemmenu.setIsEnabled(false);
		}
		
		
		CCMenuItemImage bookmenuItem = CCMenuItemImage.item("coverbu3a.png", "coverbu3.png", "coverbu3b.png", this, "gotoBook");
		CCMenu menuBook = CCMenu.menu(bookmenuItem);	
		menuBook.setPosition(CGPoint.make(265, 120));
		sp.addChild(menuBook);
		
		if(filemanager.sharedDirector().getHashmapmmdat().get("72.pngstep")==null)
		{
			
			bookmenuItem.setIsEnabled(false);
		}
		
		
		CCMenuItemImage settingmenuitem = CCMenuItemImage.item("coverbu4a.png", "coverbu4.png", this, "gotoSetting");
		CCMenu menuSetting = CCMenu.menu(settingmenuitem);	
		menuSetting.setPosition(CGPoint.make(162, 90));
		sp.addChild(menuSetting);
		
		
		CCMenuItemImage helpmenuItem = CCMenuItemImage.item("coverbu5a.png", "coverbu5.png", this, "gotoHelp");
		CCMenu menuHelp= CCMenu.menu(helpmenuItem);	
		menuHelp.setPosition(CGPoint.make(162, 30));
		sp.addChild(menuHelp);
		
	}

	public void gotoPlay(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		
		 CCScene scene = leveLayer.scene(1);
		 //CCDirector.sharedDirector().replaceScene(scene);
		 scenemanager.go(scene);
	}
	
	
	public void goONpaly(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		CCScene scene = PlayLayer.scene(_goleve);
		 //CCDirector.sharedDirector().replaceScene(scene);
		 scenemanager.go(scene);
		
	}
	
	
	public void gotoBook(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		HashMap<String, Object> tmphash=  filemanager.sharedDirector().getHashmapgameinfo();
		
		int page = Integer.parseInt(tmphash.get("bookinfo").toString()) ;
		
		CCScene scene = bookLayer.scene(page);
		// CCDirector.sharedDirector().replaceScene(scene);
		 scenemanager.go(scene);
		
	}
	
	public void gotoSetting(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		CCScene scene = SettingLayer.scene();
		// CCDirector.sharedDirector().replaceScene(scene);
		 scenemanager.go(scene);
	}
	
	public void gotoHelp(Object sender)
	{
		playSound.sharedDirector().playEffectSound(R.raw.kick);
		CCScene scene = helpLayer.scene();
		 //CCDirector.sharedDirector().replaceScene(scene);
		 scenemanager.go(scene);
	}

	
	private int getGoleve()
	{
		//playSound.sharedDirector().playEffectSound(R.raw.kick);
		int i;
		for(i = 1 ; i< 73 ; i++)
		{
			String tmpname = String.format("%d.pngstep", i);
			
			if(filemanager.sharedDirector().getHashmapmmdat().get(tmpname) == null)
			{
				
				i = i -1;
				return i;
			}
			
		}
		
		return i;
	}
	
	 public  void onExit()
	{		
			this.removeAllChildren(true);
			super.onExit();
	}
}
